package com.hnust.root.service.forword.wx;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hnust.root.common.util.ComUtil;
import com.hnust.root.dao.entity.domain.forward.SystemMessage;
import com.hnust.root.dao.entity.domain.forward.UserLike;
import com.hnust.root.dao.entity.domain.forward.UserMessage;
import com.hnust.root.dao.entity.domain.forward.WxUserInfo;
import com.hnust.root.dao.mapper.wx.SystemMessageMapper;
import com.hnust.root.dao.mapper.wx.UserLikeMapper;
import com.hnust.root.dao.mapper.wx.UserMassageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MessageService extends ServiceImpl<UserMassageMapper, UserMessage> {
    @Autowired
    private UserMassageMapper userMassageMapper;

    @Autowired
    private WxUserInfoService wxUserInfoService;

    @Autowired
    private TopicService topicService;

    @Autowired
    private SystemMessageMapper systemMessageMapper;

    //获取用户消息
    public List<UserMessage> getUserMassage(String openId) {
        List<UserMessage> userMessages = userMassageMapper.selectList(new QueryWrapper<UserMessage>()
                .eq("to_open_id", openId)
                .orderByDesc("create_time"));
        userMessages.forEach(userMessage -> {
            userMessage.setFromUserInfo(wxUserInfoService.getUserInfoByOpenId(userMessage.getFromOpenId()));
            userMessage.setToUserInfo(wxUserInfoService.getUserInfoByOpenId(userMessage.getToOpenId()));
            userMessage.setTopic(topicService.getTopicById(userMessage.getTopicId()));
        });
        return userMessages;
    }

    /**
     * 获取用户未读消息数量
     * @param openId
     * @return信息
     */
    public int getUserUnReadMsgCount(String openId) {
        List<UserMessage> userMessages = userMassageMapper.selectList(new QueryWrapper<UserMessage>()
                .eq("to_open_id", openId)
                .eq("flag", false));
        return userMessages.size();
    }

    //用户阅读消息
    public boolean readUserMessage(int msgId) {
        return ComUtil.DBisSuccess(userMassageMapper.readMessage(msgId));
    }

    //获取所有的系统消息
    public List<SystemMessage> getAllSysMsg(String openId) {
        //获取所有人可见的消息
        List<SystemMessage> allMsg = systemMessageMapper.selectList(new QueryWrapper<SystemMessage>()
                .eq("type", 0));

        WxUserInfo wxUserInfo = wxUserInfoService.getUserInfoByOpenId(openId);
        if (wxUserInfo != null) {
            List<SystemMessage> userSysMsg = new ArrayList<>();
            String university = wxUserInfo.getUniversity();
            if (!StrUtil.isEmpty(university)) {
                //根据学校推送
                userSysMsg = systemMessageMapper.selectList(new QueryWrapper<SystemMessage>()
                        .eq("university", university)
                        .eq("type", 1)
                        .orderByDesc("create_time"));
                if (!userSysMsg.isEmpty()) {
                    allMsg.addAll(userSysMsg);
                }
            }
        }
        return allMsg;
    }

    //用户阅读系统消息sys
    public void readSysMsg(int msgId) {
        SystemMessage message = systemMessageMapper.selectById(msgId);
        message.setReadCount(message.getReadCount() + 1);
        systemMessageMapper.updateById(message);
    }

    //添加用户消息
    public boolean addUserMessage(UserMessage userMessage) {
        return saveOrUpdate(userMessage);
    }

    //通过话题ID删除用户的消息
    public void deleteMsgByTopicId(int topicId) {
        userMassageMapper.delete(new QueryWrapper<UserMessage>().eq("topic_id", topicId));
    }

}
